package org.jxc.web.action;



import java.util.Date;
import java.util.List;

import org.jxc.domain.BackPurchase;
import org.jxc.domain.BackPurchaseProduct;
import org.jxc.domain.ProductWarehouseStock;
import org.jxc.service.BackPurchaseProductService;
import org.jxc.service.BackPurchaseService;
import org.jxc.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.team4.exception.CRUDException;
import org.team4.util.LoginBean;
import org.team4.web.action.GenericAction;

@Controller
@Scope("prototype")
public class BackPurchaseAction extends GenericAction<BackPurchase> {
	private static final long serialVersionUID = -4665378081525232678L;
	@Autowired
	private BackPurchaseService service;
	@Autowired
	private BackPurchaseProductService backPurchaseProductService;
	@Autowired
	private StockService stockService;
	
	private BackPurchase object ;
	private List<BackPurchaseProduct> list ;
	
	public String listPagination() throws Exception {
		page = service.listPagination(page);
		return SUCCESS;
	}

	public String toSave(){
		return SUCCESS;
	}
	
	public String save() throws CRUDException{
		object.setCreateTime(new Date());
		object.setCreator(LoginBean.getLoginBean().getUser());
		service.save(object);
		if(list !=null && list.size() > 0){
			for (int i = 0; i < list.size(); i++) {
				BackPurchaseProduct temp = list.get(i);
				temp.setBackPurchase(object);
				backPurchaseProductService.save(temp);
			}
		}
		return SUCCESS;
	}

	public String toUpdate() throws CRUDException {
		object = service.findById(object.getId());
		list = backPurchaseProductService.getListByBackPurchaseId(object.getId());
		return SUCCESS;
	}
	
	public String update() throws CRUDException{
		String code = object.getCode();
		Date backDate = object.getBackDate();
		object = service.findById(object.getId());
		object.setCode(code);
		object.setBackDate(backDate);
		service.update(object);
		
		if(list != null){
			Integer temp;
			for (int i = 0; i < list.size(); i++) {
				temp = list.get(i).getAmounts();
				BackPurchaseProduct tempBackProduct = backPurchaseProductService.findById(list.get(i).getId());
				tempBackProduct.setAmounts(temp);
				backPurchaseProductService.update(tempBackProduct);
			}
		}
		return SUCCESS;
	}
	
	public String view() throws CRUDException {
		object = service.findById(object.getId());
		list = backPurchaseProductService.getListByBackPurchaseId(object.getId());
		return SUCCESS;
	}
	
	public String outStock() throws CRUDException{
		object = service.findById(object.getId());
		object.setIsOutStock(1);
		service.update(object);
		list = backPurchaseProductService.getListByBackPurchaseId(object.getId());
		for (int i = 0; i < list.size(); i++) {
			ProductWarehouseStock stock = stockService.findById(list.get(i).getStock().getId());
			stock.setBackAmounts(stock.getBackAmounts() - list.get(i).getAmounts());
			stockService.update(stock);
		}
		return SUCCESS;
	}
	
	public String delete() throws CRUDException {
		service.delete(object.getId());
		return SUCCESS;
	}
	
	public BackPurchase getObject() {
		return object;
	}


	public void setObject(BackPurchase object) {
		this.object = object;
	}

	public List<BackPurchaseProduct> getList() {
		return list;
	}

	public void setList(List<BackPurchaseProduct> list) {
		this.list = list;
	}
	
}
